#!/bin/bash

# 设置颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== BLESDK集成验证脚本 ===${NC}"

# 导航到Example目录
echo -e "${GREEN}导航到Example目录...${NC}"
cd ~/Desktop/BLESDK/Example

# 检查BLESDK是否被正确导入
echo -e "${YELLOW}检查BLESDK导入情况...${NC}"

# 在AppDelegate中检查BLESDK导入
if grep -q "#import <BLESDK/BLESDK.h>" BLESDK/PREFIXAppDelegate.m; then
    echo -e "${GREEN}PREFIXAppDelegate.m中已导入BLESDK√${NC}"
else
    echo -e "${RED}PREFIXAppDelegate.m中未导入BLESDK!${NC}"
    echo -e "${YELLOW}正在修复...${NC}"
    sed -i '' '/#import "PREFIXAppDelegate.h"/a\'$'\n''#import <BLESDK/BLESDK.h>' BLESDK/PREFIXAppDelegate.m
fi

# 在AppDelegate中检查BLESDK初始化
if grep -q "\[BLESDK initSDK\]" BLESDK/PREFIXAppDelegate.m; then
    echo -e "${GREEN}PREFIXAppDelegate.m中已初始化BLESDK√${NC}"
else
    echo -e "${RED}PREFIXAppDelegate.m中未初始化BLESDK!${NC}"
    echo -e "${YELLOW}正在修复...${NC}"
    sed -i '' 's/- (BOOL)application:(UIApplication \*)application didFinishLaunchingWithOptions:(NSDictionary \*)launchOptions/- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\
    \/\/ 初始化BLESDK\
    [BLESDK initSDK];\
    \
    \/\/ 开启调试日志\
    [BLESDK setDebugLogEnabled:YES];\
    \
    NSLog(@"BLESDK已初始化，版本: %@", [BLESDK version]);\
    /' BLESDK/PREFIXAppDelegate.m
fi

# 在ViewController中检查BLESDK导入和使用
if grep -q "#import <BLESDK/BLESDK.h>" BLESDK/ViewController.m; then
    echo -e "${GREEN}ViewController.m中已导入BLESDK√${NC}"
else
    echo -e "${RED}ViewController.m中未导入BLESDK!${NC}"
    echo -e "${YELLOW}正在修复...${NC}"
    sed -i '' '/#import "ViewController.h"/a\'$'\n''#import <BLESDK/BLESDK.h>' BLESDK/ViewController.m
fi

# 检查是否实现了BLEManagerDelegate协议
if grep -q "BLEManagerDelegate" BLESDK/ViewController.m; then
    echo -e "${GREEN}ViewController已实现BLEManagerDelegate协议√${NC}"
else
    echo -e "${RED}ViewController未实现BLEManagerDelegate协议!${NC}"
    echo -e "${YELLOW}请检查ViewController.m文件，确保正确实现了蓝牙功能${NC}"
fi

# 检查是否在Podfile中正确引用了BLESDK
if grep -q "pod 'BLESDK', :path => '../'" Podfile; then
    echo -e "${GREEN}Podfile中已正确引用BLESDK√${NC}"
else
    echo -e "${RED}Podfile中未正确引用BLESDK!${NC}"
    echo -e "${YELLOW}请检查Podfile文件，确保包含 pod 'BLESDK', :path => '../'${NC}"
fi

# 检查是否已安装Pod
if [ -d "Pods/BLESDK" ]; then
    echo -e "${GREEN}BLESDK Pod已安装√${NC}"
else
    echo -e "${RED}BLESDK Pod未安装或安装不完整!${NC}"
    echo -e "${YELLOW}正在尝试重新安装...${NC}"
    pod install
fi

echo -e "${GREEN}BLESDK集成验证完成!${NC}"
echo -e "${YELLOW}请在Xcode中执行以下操作:${NC}"
echo -e "${YELLOW}1. 选择菜单 Product > Clean Build Folder (Shift+Command+K)${NC}"
echo -e "${YELLOW}2. 重新构建并运行项目${NC}"
echo -e "${YELLOW}3. 查看控制台日志，确认BLESDK是否已初始化并且可以扫描蓝牙设备${NC}"
echo -e "${YELLOW}4. 如需测试完整功能，请在运行时打开设备蓝牙并准备一个BLE设备${NC}" 